package com.axend.aerosense.dev.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.utils.LogUtils;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.dev.BR;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevFragmentSettingsBinding;
import com.axend.aerosense.dev.entity.DevRadarSettingResBean;
import com.axend.aerosense.dev.viewmodel.DevSettingViewModel;
import com.axend.aerosense.common.connect.bean.MessageEntity;

import java.math.BigDecimal;

/**
 * 设备参数设置
 */
public class DevAssureSettingFragment
        extends MvvmBaseFragment<DevFragmentSettingsBinding, DevSettingViewModel>
        implements ILoadDataResult<DevRadarSettingResBean> {

    private RadarBean radarBean;

    public static DevAssureSettingFragment getInstance(){
        DevAssureSettingFragment fragment = new DevAssureSettingFragment();
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null){
            radarBean = (RadarBean) bundle.getSerializable(DevDeviceSettingsFragment.ARG_PARAM);
        }
        initView();
        onRetryBtnClick();
    }

    private void initView() {
        setLoadSir(viewDataBinding.devContentParent);
        viewModel.setRadarBean(radarBean);
        viewDataBinding.devBtnSave.setOnClickListener(v -> viewModel.setSettings(getActivity()));

    }


    private void backToFirstPage(){
        LiveDatabus.getInstance()
                .with(Gobal.EVENT_KEY.CONNECT_BROKEN, Boolean.class)
                .postValue(true);

        FragmentManager.BackStackEntry entry = getActivity().getSupportFragmentManager().getBackStackEntryAt(1);
        int id = entry.getId();
        getActivity().getSupportFragmentManager().popBackStack(id, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }


    @Override
    public void onResume() {
        super.onResume();
        ((ITitle)getActivity()).setTitle(getString(R.string.dev_param_setting));
    }

    @Override
    public int getLayoutId() {
        return R.layout.dev_fragment_settings;
    }

    @Override
    public int getBindingVariable() {
        return BR.data;
    }

    @Override
    protected DevSettingViewModel getViewModel() {
        BaseViewModelFactroy<DevRadarSettingResBean> myViewModelFactory = new BaseViewModelFactroy<>(this, this);
        return new ViewModelProvider(this, myViewModelFactory).get(DevSettingViewModel.class);
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if(hidden){
//            viewDataBinding.devEalyWarningSeekbar.setVisibility(View.INVISIBLE);
            viewDataBinding.devWorkDistanceSeekbar.setVisibility(View.INVISIBLE);
        }else{
//            viewDataBinding.devEalyWarningSeekbar.setVisibility(View.VISIBLE);
            viewDataBinding.devWorkDistanceSeekbar.setVisibility(View.VISIBLE);
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void loadDataSuccess(DevRadarSettingResBean bean) {
        if (null == bean || null == bean.getRadarInfo()) return;

        viewModel.setBufferTime(bean.getRadarInfo().getAlarmDelay());
        viewModel.setWorkDistance(bean.getRadarInfo().getWorkingDistance());
        viewModel.setInstallHeight(bean.getRadarInfo().getInstallationHeight());
    }

    @Override
    public void loadDataFail(String decs) {

    }
}